package com.example.clientmessage;

import java.util.Date;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.example.utils.LocalDB;

public class ChatActivity extends Activity {

	LocalDB localDB = new LocalDB(this);

	TableLayout tl;

	private String cellNo;

	LinearLayout.LayoutParams layoutParams;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chat);

		Intent intent = getIntent();
		cellNo = intent.getStringExtra(ContactActivity.CELL_NO);

		TextView textView = (TextView) findViewById(R.id.contactName);
		textView.setText(cellNo);

		TableRow tr1;

		tl = (TableLayout) findViewById(R.id.messageLayout);
		Display display = getWindowManager().getDefaultDisplay();
		int width = display.getWidth();

		layoutParams = new LinearLayout.LayoutParams(width,
				LayoutParams.FILL_PARENT);

		// for (int i = 0; i < 30; i++) {
		//
		// tr1 = (TableRow) new TableRow(this);
		// TextView txt9 = new TextView(this);
		// txt9.setText("test" + i);
		// tr1.addView(txt9);
		// tl.addView(tr1, new TableLayout.LayoutParams(layoutParams));
		// }

		// Read from database

		Cursor cursor = this.getMessage();
		while (cursor.moveToNext()) {
			tr1 = (TableRow) new TableRow(this);
			TextView txt9 = new TextView(this);
			String string = cursor.getString(0);
			txt9.setText(string);
			tr1.addView(txt9);
			tl.addView(tr1, new TableLayout.LayoutParams(layoutParams));
		}
		cursor.close();
	}

	private Cursor getMessage() {
		String[] tableColumns = new String[] { LocalDB.MESSAGE };
		String whereClause = LocalDB.CELL_NO + "=?";
		String[] whereArgs = new String[] { cellNo };
		SQLiteDatabase db = localDB.getReadableDatabase();
		Cursor cursor = db.query(LocalDB.TABLE_NAME, tableColumns, whereClause,
				whereArgs, null, null, null);
		startManagingCursor(cursor);
		return cursor;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_chat, menu);
		return true;
	}

	public void sendMessage(View view) {
		EditText sendText = (EditText) findViewById(R.id.sendTextbox);
		TableRow newMessage = (TableRow) new TableRow(this);
		TextView txt9 = new TextView(this);
		Editable text = sendText.getText();
		txt9.setText(text);
		newMessage.addView(txt9);
		tl.addView(newMessage, new TableLayout.LayoutParams(layoutParams));

		sendText.setText("");
		((ScrollView) findViewById(R.id.contactScrollView))
				.post(new Runnable() {
					public void run() {
						((ScrollView) findViewById(R.id.contactScrollView))
								.fullScroll(View.FOCUS_DOWN);
					}

				});

		// Save to db

		SQLiteDatabase db = localDB.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(LocalDB.CELL_NO, cellNo);
		values.put(LocalDB.MESSAGE, text.toString());
		values.put(LocalDB.TIMESTAMP, (new Date()).getTime());
		db.insert("message_log", null, values);
	}

}
